/*
 * @Descripttion: 
 * @version: 
 * @Author: cyj
 * @Date: 2023-01-07 23:52:52
 */
import { defineStore } from 'pinia'

export const useMusicStore = defineStore('music', {
    state: () => {
        return {
            //当前歌曲播放地址
            songUrl: '',
            //歌曲播放状态
            isplaying: false,
            //当前播放歌曲的详细信息
            nowSongDetail: {},
            //历史播放列表
            historyPlay: [],
            // 是否显示当前歌曲详情页
            isShowSongDetail: false,
            //播放模式  默认是循环播放
            playModel: 1,
            //正在播放列表
            playingList: [],
            //歌曲播放实时秒数
            currentSecond: 0,
            //歌曲时间进度条
            songProgress: 0,
            //歌曲总时间
            totalTime: '00:00',
            //歌曲总秒数
            totalSecond: 0,
            //歌曲当前处于的时间(分钟)
            currentTime: '00:00',
            //声音进度条  默认是50
            voiceProgress: 50,
            //声音临时音量
            tempVoice: 0,

            //当前播放的歌曲的歌词
            currentLyric: [],
            //是否展示歌词
            isShowLyric: false,
            //当前歌词行
            currentRow: 0,

        }
    },
    getters: {

    },
    actions: {
        //修改音乐播放状态  type 布尔值
        changePlayState(type) {
            this.isplaying = type
        },

        //保存当前歌曲的url
        saveSongUrl(url) {
            this.songUrl = url
        },

        // 保存当前歌曲详情，并添加到播放历史记录中
        SaveSongDetailAndaddHistoryPlayList(song) {
            this.nowSongDetail = song
            let index = this.historyPlay.findIndex(item => {
                return item.id == song.id
            })
            if (index === -1)
                //把歌曲添加进历史播放记录
                this.historyPlay.unshift(song)
        },

        //把当前歌曲添加进当前播放列表中
        addPlayingList(song) {
            let index = this.playingList.findIndex(item => {
                return song.id == item.id
            })
            //如果播放列表中没有当前歌曲
            if (index === -1)
                this.playingList.unshift(song)
        },

        //删除当前列表中的某一个歌曲
        deletePlayingSong(song) {
            //删除选中的歌曲
            this.playingList = this.playingList.filter(item => {
                return item.id !== song.id
            })
            //如果删除完之后，播放列表为空
            if (this.playingList.length === 0) {
                //清空当前播放
                this.nowSongDetail = {}
                //清空歌曲url
                this.isplaying = false
                this.songUrl = {}
                this.currentSecond = 0
                this.currentTime = '00:00'
                this.totalTime = '00:00'
                this.totalSecond = 0
                this.songProgress = 0
            }
        },

        //清空当前播放列表
        clearPlayingList() {
            //清空当前列表
            this.playingList = []
            //清空当前播放
            this.nowSongDetail = {}
            //清空歌曲url
            this.isplaying = false
            this.songUrl = {}
            this.currentSecond = 0
            this.currentTime = '00:00'
            this.totalTime = '00:00'
            this.totalSecond = 0
            this.songProgress = 0
        },

        //删除历史列表中的某一首歌
        deleteHistoryList(song) {
            this.historyPlay = this.historyPlay.filter(item => {
                return song.id !== item.id
            })
        },

        //清空历史播放列表
        clearHistoryList() {
            this.historyPlay = []

        },

        //点击播放全部，把专辑中所有歌曲添加到当前播放列表中
        addAllSongs(songs) {
            this.clearPlayingList()  //先清空当前播放列表相关数据
            this.playingList = songs
        },

        //保存当前播放歌曲的歌词
        saveCurrentLyric(data) {
            this.currentLyric = data
        },

        //是否展示浮动歌词
        isShowFloatLyric() {
            this.isShowLyric = !this.isShowLyric
        },
        //保存当前歌词行
        saveCurrentRow(row) {
            this.currentRow = row
        }
    },
    persist: {
        enabled: true,
        strategies: [
            { storage: localStorage }
        ]
    }
})